<template>
  <div class="custom-table-list-wrapper">
    <div class="search" :class="{ active: toggleSearchStatus }">
      <div class="search-left" ref="searchFromRef">
        <el-form class="search-form" inline>
          <el-form-item label="状态">
            <el-select
              class="search-select"
              v-model="searchParams.status"
              placeholder="请选择"
              multiple
            >
              <el-option
                v-for="(item, index) of dict['reservationExamineStatus']"
                :key="index"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="预约时段">
            <el-date-picker
              v-model="timeArr"
              type="daterange"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD"
              date-format="YYYY-MM-DD"
              @change="handleChangeTime"
            />
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              class="search-input"
              v-model="searchParams.keywords"
              placeholder="请输入访客姓名、被访单位、被访人员"
              :suffix-icon="Search"
            ></el-input>
          </el-form-item>
        </el-form>
        <a
          class="search-left-toggle"
          v-if="showToggle"
          @click="handleToggleSearch"
        >
          <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
          {{ toggleSearchStatus ? '收起' : '展开' }}
        </a>
      </div>
      <div class="search-right">
        <el-button type="primary" @click="initTableList(1)"
          ><svg-icon name="search" size="14px" />搜索</el-button
        >
        <el-button @click="handleResetCustom"
          ><svg-icon name="reset" size="17px" />重置</el-button
        >
      </div>
    </div>
    <div class="content">
      <div class="content-table">
        <el-table class="custom-table" :data="tableData">
          <el-table-column type="index" label="序号" width="60px" />
          <el-table-column prop="name" label="访客姓名" />
          <el-table-column prop="phone" label="联系方式">
            <template #default="scope">
              {{
                scope.row.phone.replace(/^(\d{3})(\d{4})(\d{4})$/, '$1****$3')
              }}
            </template>
          </el-table-column>
          <el-table-column prop="visitedUnitName" label="被访单位" />
          <el-table-column prop="visitedUserName" label="被访人员" />
          <el-table-column prop="updateTime" label="预约时段">
            <template #default="scope">
              {{ scope.row.startTime }} - {{ scope.row.endTime }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="提交时间" />
          <el-table-column prop="status" label="状态">
            <template #default="scope">
              <el-popover placement="bottom" :content="scope.row.applyCause">
                <template #reference>
                  <el-tag
                    :type="
                      getDictStyle('reservationExamineStatus', scope.row.status)
                    "
                    >{{
                      getDictValue('reservationExamineStatus', scope.row.status)
                    }}</el-tag
                  >
                </template>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120px">
            <template #default="scope">
              <el-button link type="primary" @click="handleDetail(scope.row)"
                >详情</el-button
              >
              <el-button
                link
                type="primary"
                @click="handleExamine(scope.row)"
                v-if="scope.row.status === -2"
                >审核</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="content-page">
        <el-pagination
          class="custom-pagination"
          :current-page="paginationParams.pageIndex"
          :page-size="paginationParams.pageSize"
          background
          :page-sizes="[10, 20, 30, 50]"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="initTableList"
        />
      </div>
    </div>
    <DetailDialog
      ref="detailRef"
      :detail="currentDetail"
      @close="handleClose"
    />
    <ExamineDialog
      ref="examineRef"
      :detail="currentDetail"
      @close="handleClose"
    />
  </div>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import DetailDialog from './detail.vue'
import ExamineDialog from './examineDialog.vue'
import { useTableList } from '@/hooks/useTableList'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getReservationRecordList } from '@/api/manager/visitorManage/reservationRecord.js'
import { useDict } from '@/hooks/useDict'
const {
  dict,
  dictOptions,
  initListDictOptions,
  initTreeDictOptions,
  getDictValue,
  getDictStyle,
} = useDict(['reservationExamineStatus'])
const searchParams = ref({
  status: [],
})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams, getReservationRecordList)

const timeArr = ref([])
const handleChangeTime = () => {
  if (timeArr.value && timeArr.value.length > 0) {
    searchParams.value.startTime = timeArr.value[0]
    searchParams.value.endTime = timeArr.value[1]
  } else {
    searchParams.value.startTime = ''
    searchParams.value.endTime = ''
  }
}
const handleResetCustom = () => {
  timeArr.value = []
  searchParams.value.status = []
  const params = { status: [] }
  handleReset(params)
}

const examineRef = ref()
const handleExamine = (row) => {
  currentDetail.value = row
  examineRef.value.dialogVisible = true
}
onMounted(() => {
  initTableList(1)
})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';
.content-table {
  height: calc(100% - 50px);
}
</style>
